from typing import List
from ..utils.file import save_upload_file, extract_zip, get_file_info, validate_file
from ..utils.logger import setup_logger

logger = setup_logger('upload_service')

class UploadService:
    @staticmethod
    def handle_upload(file_content: bytes, filename: str) -> List[str]:
        """处理文件上传"""
        try:
            # 保存上传的文件
            file_path = save_upload_file(file_content, filename)
            
            # 获取文件信息
            _, file_type, file_size = get_file_info(file_path)
            
            # 验证文件
            if not validate_file(file_type, file_size):
                raise ValueError("不支持的文件类型或文件过大")
            
            # 如果是ZIP文件，解压并获取PDF文件列表
            if file_type == 'zip':
                pdf_files = extract_zip(file_path)
                logger.info(f"解压完成，找到{len(pdf_files)}个PDF文件")
                return pdf_files
            else:
                return [file_path]
                
        except Exception as e:
            logger.error(f"文件上传处理失败: {str(e)}")
            raise 